package com.wing.setting.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.setting.dao.SysSettingDao;
import com.wing.setting.model.entity.SysSetting;
import com.wing.setting.model.entity.SysSettingAdvert;
import com.wing.setting.model.request.SysSettingForm;
import com.wing.setting.model.response.SysSettingVO;
import com.wing.setting.model.response.SystemOfficialInfoVO;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 刀具柜系统设置-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class SysSettingServiceImpl extends ServiceImpl<SysSettingDao, SysSetting> implements SysSettingService {

    @Autowired
    private SysSettingAdvertService advertService;

    @Override
    @Transactional
    public int add(SysSettingForm sysSettingForm) {

        sysSettingForm.setEnterpriseCode(JwtUtils.getEnterpriseCode());

        //校验
        //验证数据是否已存在
        SysSetting selectOne = this.getOne(sysSettingForm.getType().getValue(),sysSettingForm.getEnterpriseCode());

        if (ObjectUtils.isNotEmpty(selectOne)){
            //修改
            SysSetting updateSysSetting = new SysSetting();
            updateSysSetting.setId(selectOne.getId());
            updateSysSetting.setAdvertVideo(sysSettingForm.getAdvertVideo());

            if (baseMapper.updateById(updateSysSetting)<=0){
                throw new BusinessException("视频修改失败");
            }
        } else {

            SysSetting sysSetting = new SysSetting();
            CopyOptions copyOptions = CopyOptions.create()
                    .setEditable(SysSetting.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(sysSettingForm, sysSetting, copyOptions);

            if (baseMapper.insert(sysSetting) <= 0) {
                throw new BusinessException("添加异常");
            }
        }
        return 1;
    }

    @Override
    public List<SysSettingVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public SysSettingVO settingList() {
        SysSettingVO vos = baseMapper.settingList();

        QueryWrapper<SysSettingAdvert> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setting_id",vos.getId());
        queryWrapper.eq("is_deleted", 0);
        List<SysSettingAdvert> advertList = advertService.list(queryWrapper);
        vos.setAdvert(advertList);

        return vos;
    }

    @Override
    public Page<SysSettingVO> pageList4app(Page page, Map<String, Object> params) {
        List<SysSettingVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public SysSettingVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public SysSettingVO getSettingAdvertInfo() {
        SysSettingVO vos = baseMapper.getSetting();
        if (vos!=null){
            QueryWrapper<SysSettingAdvert> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("setting_id",vos.getId());
            queryWrapper.eq("is_deleted", 0);
            List<SysSettingAdvert> advertList = advertService.list(queryWrapper);
            vos.setAdvert(advertList);
        }

        return vos;
    }

    @Override
    public List<SysSettingVO> getSettingListAdmin() {
        List<SysSettingVO> list = baseMapper.getSettingListAdmin();
        for (int i = 0; i < list.size(); i++) {
            QueryWrapper<SysSettingAdvert> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("setting_id", list.get(i).getId());
//            queryWrapper.eq("is_deleted", 0);
            List<SysSettingAdvert> advertList = advertService.list(queryWrapper);
            list.get(i).setAdvert(advertList);
        }
        return list;
    }

    @Override
    public SysSettingVO getAdvertList() {
        SysSettingVO vos = baseMapper.getAdvertList();


        QueryWrapper<SysSettingAdvert> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setting_id",vos.getId());
        queryWrapper.eq("is_deleted", 0);
        List<SysSettingAdvert> advertList = advertService.list(queryWrapper);
        vos.setAdvert(advertList);

        return vos;
    }



    @Override
    public Boolean upSetting(SysSettingVO sysSettingVO) {

        SysSettingAdvert sysSettingAdvert = new SysSettingAdvert();
        BeanUtil.copyProperties(sysSettingVO, sysSettingAdvert);

        if (!advertService.updateById(sysSettingAdvert)) {
            return false;
        }
        return true;
    }

    @Override
    public SysSettingVO getSettingCabInfo() {
        SysSettingVO vos = baseMapper.getSettingCab();

        if(Objects.isNull(vos)){
            return new SysSettingVO();
        }else{
            QueryWrapper<SysSettingAdvert> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("setting_id",vos.getId());
//        queryWrapper.eq("is_deleted", 0);
            List<SysSettingAdvert> advertList = advertService.list(queryWrapper);
            if(null != advertList) {
                for (SysSettingAdvert sysSettingAdvert :advertList) {
                    sysSettingAdvert.setAdvertVideo(vos.getAdvertVideo());
                }
            }
            vos.setAdvert(advertList);
        }
        return vos;
    }



    @Override
    @Transactional
    public boolean addSetting(List<SysSettingForm> settingForm) {
        SysSetting setting = new SysSetting();
        for (int i = 0; i <settingForm.size() ; i++) {
            BeanUtil.copyProperties(settingForm.get(i), setting);
            setting.setId(null);
            if (!this.save(setting)) {
                return false;
            }
            settingForm.get(i).getAdvert().parallelStream().forEach(k ->{
                SysSettingAdvert advert = k;
                advert.setId(null);
                advert.setSettingId(setting.getId());
                if (!advertService.save(advert)) {
                }
            });
        }
        return true;
    }

    @Override
    public  SysSettingVO getStartSetting(Map<String, Object> params) {


        List<SysSettingVO> vos = baseMapper.getStartSetting(params);
        QueryWrapper<SysSettingAdvert> advertQueryWrapper = new QueryWrapper<>();
        if (vos.size()!=0){
            advertQueryWrapper.eq("setting_id",vos.get(0).getId());
            advertQueryWrapper.eq("is_deleted", 0);
            List<SysSettingAdvert> advertList = advertService.list(advertQueryWrapper);
            vos.get(0).setAdvert(advertList);
            return vos.get(0);
        }else {
            params.put("enterpriseId",null);
            List<SysSettingVO> sysSettingVOS = baseMapper.getStartSetting(params);
            if (sysSettingVOS.size()==0){
                return null;
            }
            advertQueryWrapper.eq("setting_id",sysSettingVOS.get(0).getId());
            advertQueryWrapper.eq("is_deleted", 0);
            List<SysSettingAdvert> advertList = advertService.list(advertQueryWrapper);
            sysSettingVOS.get(0).setAdvert(advertList);
            return sysSettingVOS.get(0);
        }



    }

    @Transactional
    @Override
    public JsonResult addSettingList(SysSettingVO sysSettingVO) {
        SysSetting setting = new SysSetting();
        BeanUtil.copyProperties(sysSettingVO,setting);
        setting.setId(null);
        setting.setCreateTime(LocalDateTime.now());
        setting.setType(SysSetting.Type.startCab);
        setting.setEnterpriseCode(JwtUtils.getEnterpriseCode());

        //验证数据是否已存在
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_code",sysSettingVO.getEnterpriseCode());
        queryWrapper.eq("type",sysSettingVO.getType());
        int count = baseMapper.selectCount(queryWrapper);
        if (count<0){
//            return JsonResult.fail("该企业已存在柜子启动广告设置信息");
            if (!this.save(setting)){
                return JsonResult.fail("添加失败");
            }
        }


        for (int i = 0; i < sysSettingVO.getAdvert().size(); i++) {
            SysSettingAdvert sysSettingAdvert =sysSettingVO.getAdvert().get(i);
            sysSettingAdvert.setSettingId(setting.getId());
            sysSettingAdvert.setCreateTime(LocalDateTime.now());
            if (!advertService.save(sysSettingAdvert)){
                return JsonResult.fail("添加失败");
            }
        }
        return JsonResult.success("添加成功");
    }

    @Override
    public List<SysSettingVO> getSettingListByParam(Map<String, Object> params) {
        return baseMapper.getSettingListByParam(params);
    }

    @Override
    public SysSettingVO getVideoByChannelCode(String channelCode) {
        return baseMapper.getVideoByChannelCode(channelCode);
    }

    @Override
    public SysSetting getOne(Integer type, String enterpriseCode) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_code",enterpriseCode);
        queryWrapper.eq("type",type);
        SysSetting sysSetting = baseMapper.selectOne(queryWrapper);
        return sysSetting;
    }

    @Override
    public SystemOfficialInfoVO getSystemOfficialInfo() {
        return baseMapper.getSystemOfficialInfo();
    }
}
